package com.smartfactory.sail.controller;

import com.ada.base.common.ApiResponse;
import com.ada.base.common.ControllerResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smartfactory.sail.entity.Purchase;
import com.smartfactory.sail.entity.PurchaseRecord;
import com.smartfactory.sail.entity.StorageRecord;
import com.smartfactory.sail.entity.enums.PurchaseStatusEnum;
import com.smartfactory.sail.entity.enums.StorageStatusEnum;
import com.smartfactory.sail.entity.enums.StorageType;
import com.smartfactory.sail.mapper.StorageRecordMapper;
import com.smartfactory.sail.service.IItemService;
import com.smartfactory.sail.service.IOrderService;
import com.smartfactory.sail.service.IPurchaseRecordService;
import com.smartfactory.sail.service.IPurchaseService;
import com.smartfactory.sail.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 王巨壁
 * @title: StatusController
 * @projectName smart-sail
 * @description: 状态相关的
 * @date 2019/9/321:38
 */
@Api(tags = "状态管理")
@RequestMapping("/status")
@RestController
public class StatusController extends ControllerResponse {
    private IItemService iitemService;
    private IOrderService iOrderService;
    private IPurchaseRecordService iPurchaseRecordService;
    private StorageRecordMapper storageRecordMapper;
    private IPurchaseService iPurchaseService;

    public StatusController(IItemService iitemService, IOrderService iOrderService, IPurchaseRecordService iPurchaseRecordService, StorageRecordMapper storageRecordMapper, IPurchaseService iPurchaseService) {
        this.iitemService = iitemService;
        this.iOrderService = iOrderService;
        this.iPurchaseRecordService = iPurchaseRecordService;
        this.storageRecordMapper = storageRecordMapper;
        this.iPurchaseService = iPurchaseService;
    }

    @ApiOperation(value = "产品大列表 - 修改产品在产或者停产",notes = "产品大列表 - 修改产品在产或者停产")
    @GetMapping("product/{itemId}/{status}")
    public ApiResponse<Object> product(@PathVariable("itemId") Long itemId, @PathVariable("status") String status) {
        if (iitemService.updateProductStatus(itemId, status)) {
            return ok();
        } else {
            return fail("修改失败");
        }
    }

    @ApiOperation(value = "样品订单完成",notes = "样品订单完成")
    @GetMapping("sampleOrder/{orderId}")
    public ApiResponse<Object> product(@PathVariable("orderId") Long orderId) {
        if (iOrderService.updateSampleOrder(orderId)) {
            return ok();
        } else {
            return fail("修改失败");
        }
    }

    @ApiOperation(value = "采购管理-申请入库",notes = "采购管理-申请入库")
    @GetMapping("purchase/instorage/{purchaseId}")
    public ApiResponse<Object> purchaseInstorag(@PathVariable("purchaseId") Long purchaseId) {
        PurchaseRecord byId = iPurchaseRecordService.getById(purchaseId);
        PurchaseStatusEnum status = byId.getStatus();
        if (!PurchaseStatusEnum.CGZ.equals(status)) {
            return fail("不是采购中状态,不允许申请发货!");
        }
        byId.setStatus(PurchaseStatusEnum.DRK);
        byId.setActualArrivalTime(DateUtils.getNowDate());//实际到货时间
        if (iPurchaseRecordService.updateById(byId)) {
            LambdaQueryWrapper<StorageRecord> storageQuery = new LambdaQueryWrapper<>();
            storageQuery.eq(StorageRecord::getItemId, byId.getItemId());
            storageQuery.eq(StorageRecord::getPurchaseRecordId,byId.getId());
            List<StorageRecord> records = storageRecordMapper.selectList(storageQuery);
            if (records != null && records.size() > 0) {
                storageRecordMapper.updateById(records.get(0).setStatus(StorageStatusEnum.DRK));
            } else {
                Purchase purchase = iPurchaseService.getById(byId.getPurchaseId());
                StorageRecord storageRecord = new StorageRecord();
                storageRecord.setStorageType(StorageType.YLK);
                storageRecord.setItemId(byId.getItemId());
                storageRecord.setOrderId(purchase.getObjId());
                storageRecord.setPurchaseRecordId(byId.getId());
                storageRecord.setSize(byId.getPurchaseSize());
                storageRecord.setStatus(StorageStatusEnum.DRK);
                storageRecordMapper.insert(storageRecord);
            }
            return ok();
        } else {
            return fail("申请失败!");
        }
    }
}
